package com.xy.manage.action;



import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.math.NumberUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.octo.captcha.service.CaptchaServiceException;
import com.octo.captcha.service.image.ImageCaptchaService;
import com.xy.common.page.Pagination;
import com.xy.common.util.DictUtils;
import com.xy.common.security.BadCredentialsException;
import com.xy.common.security.UsernameNotFoundException;
import com.xy.common.web.CookieUtils;
import com.xy.common.web.RequestUtils;
import com.xy.core.web.WebErrors;

import com.xy.manage.model.Tsuggestion;
import com.xy.manage.service.TsuggestionService;



@Controller
public class TsuggestionAction{
	private static final Logger log = LoggerFactory.getLogger(TsuggestionAction.class);
	public TsuggestionAction() {
	}
 
 
 	/**
	 * 意见收集新增页面
	 * 
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping("v_tsuggestionAdd.do")
	public String add(HttpServletRequest request, ModelMap model) {
		model.addAttribute("tsuggestion", new Tsuggestion());
        setDict(model);
		return "tsuggestionEdit";
	}

	/**
	 * 意见收集新增保存
	 * 
	 * @param tsuggestion
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping("/o_tsuggestionSave.do")
	public String save(Tsuggestion tsuggestion, HttpServletRequest request, ModelMap model) {
		WebErrors errors = validateSave(tsuggestion, request);
		if (errors.hasErrors()) {
			return errors.showErrorPage(model);
		}
		tsuggestion = tsuggestionService.save(tsuggestion);
		return "redirect:v_tsuggestionList.do";
	}

	/**
	 * 意见收集删除
	 * 
	 * @param ids
	 * @return
	 */
	@RequestMapping("o_tsuggestionDelete.do")
	public String delete(long[] ids) {
		if (ids != null) {
			for (long id : ids) {
				tsuggestionService.remove(id);
			}
		}
		return "redirect:v_tsuggestionList.do";
	}

	/**
	 * 意见收集编辑页面
	 * 
	 * @param sgid
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping("v_tsuggestionEdit.do")
	public String edit(long sgid, HttpServletRequest request, ModelMap model) {
		Tsuggestion tsuggestion = tsuggestionService.get(sgid);
		model.addAttribute("tsuggestion", tsuggestion);
      model.addAttribute("actionEdit", request.getParameter("request"));
        setDict(model);
		return "tsuggestionEdit";
	}

	/**
	 * 意见收集详情
	 * 
	 * @param sgid
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping("v_tsuggestionDetail.do")
	public String detail(long sgid, HttpServletRequest request, ModelMap model) {
		Tsuggestion tsuggestion = tsuggestionService.get(sgid);
		model.addAttribute("tsuggestion", tsuggestion);
        setDict(model);
		return "tsuggestionView";
	}

	/**
	 * 意见收集查询（分页）
	 * 
	 * @param tsuggestion
	 * @param pageNo
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping("v_tsuggestionList.do")
	public String listPage(Tsuggestion tsuggestion, Integer pageNo,
			HttpServletRequest request, ModelMap model) {
		if (pageNo == null)
			pageNo = 1;
		Pagination pagination = tsuggestionService.getTsuggestionByPage(toMap(tsuggestion),
				pageNo, CookieUtils.getPageSize(request));
		model.addAttribute("pagination", pagination);
        setDict(model);
		return "tsuggestionList";
	}

	/**
	 * 意见收集查询
	 * 
	 * @param tsuggestion
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping("v_tsuggestionList1.do")
	public String list(Tsuggestion tsuggestion, HttpServletRequest request, ModelMap model) {
		List<Tsuggestion> list = tsuggestionService.getTsuggestion(toMap(tsuggestion));
		model.addAttribute("list", list);
        setDict(model);
		return "tsuggestionList";
	}


 	/***
	 * 将页面输入信息转成map
	 * 
	 * @param tsuggestion
	 * @return
	 */	private Map<String,Object> toMap(Tsuggestion tsuggestion){
		Map<String,Object> map=new HashMap<String, Object>();
      		if(tsuggestion.getSgid()!=null&&!tsuggestion.getSgid().equals("")){
			map.put("Sgid", tsuggestion.getSgid());
		}
		if(tsuggestion.getUsername()!=null&&!tsuggestion.getUsername().equals("")){
			map.put("Username", tsuggestion.getUsername());
		}
		if(tsuggestion.getYJJY()!=null&&!tsuggestion.getYJJY().equals("")){
			map.put("YJJY", tsuggestion.getYJJY());
		}
		if(tsuggestion.getPF()!=null&&!tsuggestion.getPF().equals("")){
			map.put("PF", tsuggestion.getPF());
		}
		if(tsuggestion.getCreatetime()!=null&&!tsuggestion.getCreatetime().equals("")){
			map.put("Createtime", tsuggestion.getCreatetime());
		}      
		return map;
	}
   
	/**
	 * 设置页面所需的数据字典
	 * @param model
	 */
	private void setDict(ModelMap model){
		
	}

	/**
	 * 数据校验
	 * @param model
	 */
	private WebErrors validateSave(Tsuggestion tsuggestion, HttpServletRequest request) {
		WebErrors errors = WebErrors.create(request);
		/**if (errors.ifOutOfLength(username, "username", 1, 100)) {
			return errors;
		}  */    
		return errors;
	}

   
      
	@Resource
	private TsuggestionService tsuggestionService;   
}